import java.util.*;
import org.dom4j.*;

public class testxml {

	public static void main(String[] args) {
		/*String xml = 
			" <file>                                " + 
            "     <chapter id=\"1\" name=\"a\"/>    " +
            "     <chapter id=\"2\" name=\"b\"/>    " +
            "     <chapter id=\"3\" name=\"c\"/>    " +
            "     <chapter id=\"4\" name=\"d\"/>    " +
            "  </file> "                            ;
		try {
			//生产doc文档
			Document doc = DocumentHelper.parseText(xml);
			//得到根节点
			Element root = doc.getRootElement();
			//得到根节点下的chapter节点
			List elements = root.elements("chapter");
			//遍历chapter节点
			Iterator iterator = elements.iterator();		
			while(iterator.hasNext()){
				Element e = (Element)iterator.next();
				String id = e.attributeValue("id");
				//如果id==1 则添加新的节点
				if("1".equals(id) || "1" == id){
					//新添加的节点
					Element enew = e.addElement("chapterNew");
					//添加属性和值
					enew.addAttribute("id", "1.1");
					enew.addAttribute("name", "a.1");
				}
			}
			//打印
			System.out.println(doc.asXML());
			System.out.println();
		} catch (DocumentException e) {
			e.printStackTrace();
		}*/
		
		create();
		
		
	}
	
	public static void create(){
		DocumentFactory factory = DocumentFactory.getInstance();//singleton
		//添加模型配置
		Element element1 = factory.createElement("typeAlias");
		element1.addAttribute("alias", "FeatureType");
		element1.addAttribute("type", "com.wandar.myshop.biz.product.model.FeatureType");
		
		//添加mapper 配置文件 
		Element element2 = factory.createElement("mapper");
		element2.addAttribute("resource", "mapper/party/PartyMapper.xml");
		System.out.println(element1.asXML());
		System.out.println(element2.asXML());
		
		Comment comment = factory.createComment("aaaaa");
		System.out.println(comment.asXML());
		
		//添加 ibatis
		Element element3 = factory.createElement("bean");
		element3.addAttribute("id", "geoMapper");
		element3.addAttribute("class", "org.mybatis.spring.mapper.MapperFactoryBean");
		
		Element element31 =  element3.addElement("property");
		element31.addAttribute("name", "mapperInterface");
		element31.addAttribute("value", "com.wandar.myshop.biz.geo.mappler.GeoMapper");
		
		Element element32 = element3.addElement("property");
		element32.addAttribute("name", "sqlSessionFactory");
		element32.addAttribute("ref", "sqlSessionFactory");
		System.out.println(element3.asXML());
	}
}

